package com.evenmo.config;



import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
/**
 * druid Configuration+@Bean配置方式
 * @author Administrator
 *
 */
//@Configuration
//扫描 Mapper 接口并容器管理
//根据情况要修改basePackages
//@MapperScan(basePackages = "com.evenmo.mapper", sqlSessionFactoryRef = "sqlSessionFactory")
public class DataSourceConfig {
//	// 精确到 master 目录，以便跟其他数据源隔离
//	@Value("${mybatis.mapper-package}")
//    private String PACKAGE;
//	@Value("${mybatis.mapper-locations}")
//	private String MAPPER_LOCATION;
//	@Value("${mybatis.typeAliasesPackage}")
//    private String TYPE_ALIASES_PACKAGE;
//    @Value("${spring.datasource.url}")
//    private String url;
//    @Value("${spring.datasource.username}")
//    private String user;
//    @Value("${spring.datasource.password}")
//    private String password;
//    @Value("${spring.datasource.driver-class-name}")
//    private String driverClass;
//    
//    @Value("${spring.datasource.maxActive}")
//    private Integer maxActive;
//    @Value("${spring.datasource.minIdle}")
//    private Integer minIdle;
//    @Value("${spring.datasource.initialSize}")
//    private Integer initialSize;
//    @Value("${spring.datasource.maxWait}")
//    private Long maxWait;
//    @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
//    private Long timeBetweenEvictionRunsMillis;
//    @Value("${spring.datasource.minEvictableIdleTimeMillis}")
//    private Long minEvictableIdleTimeMillis;
//    @Value("${spring.datasource.testWhileIdle}")
//    private Boolean testWhileIdle;
//    @Value("${spring.datasource.testWhileIdle}")
//    private Boolean testOnBorrow;
//    @Value("${spring.datasource.testOnBorrow}")
//    private Boolean testOnReturn;
// 
//    @Bean(name = "dataSource")
//    @Primary
//    public DataSource dataSource() {
//    	//jdbc配置
//        DruidDataSource dataSource = new DruidDataSource();
//        dataSource.setDriverClassName(driverClass);
//        dataSource.setUrl(url);
//        dataSource.setUsername(user);
//        dataSource.setPassword(password);
//        
//        //连接池配置
//        dataSource.setMaxActive(maxActive);
//        dataSource.setMinIdle(minIdle);
//        dataSource.setInitialSize(initialSize);
//        dataSource.setMaxWait(maxWait);
//        dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
//        dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
//        dataSource.setTestWhileIdle(testWhileIdle);
//        dataSource.setTestOnBorrow(testOnBorrow);
//        dataSource.setTestOnReturn(testOnReturn);
//        dataSource.setValidationQuery("SELECT 'x'");
//        
//        dataSource.setPoolPreparedStatements(true);
//        dataSource.setMaxPoolPreparedStatementPerConnectionSize(20);
//        
//        try {
//			dataSource.setFilters("stat");
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//        return dataSource;
//    }
// 
//    @Bean(name = "transactionManager")
//    @Primary
//    public DataSourceTransactionManager transactionManager() {
//        return new DataSourceTransactionManager(dataSource());
//    }
// 
//    @Bean(name = "sqlSessionFactory")
//    @Primary
//    public SqlSessionFactory ds1SqlSessionFactory(@Qualifier("dataSource") DataSource dataSource)
//            throws Exception {
//        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
//        sessionFactory.setDataSource(dataSource);
//        sessionFactory.setTypeAliasesPackage(TYPE_ALIASES_PACKAGE);
//        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
//                .getResources(MAPPER_LOCATION));
//        return sessionFactory.getObject();
//    }
//    //druid监控
//    @Bean
//    public ServletRegistrationBean druidServlet() {
//      ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
//      servletRegistrationBean.setServlet(new StatViewServlet());
//      servletRegistrationBean.addUrlMappings("/druid/*");
//      Map<String, String> initParameters = new HashMap<String, String>();
//      initParameters.put("loginUsername", "admin");// 用户名
//      initParameters.put("loginPassword", "moyongjun");// 密码
//      initParameters.put("resetEnable", "false");// 禁用HTML页面上的“Reset All”功能
//      initParameters.put("allow", ""); // IP白名单 (没有配置或者为空，则允许所有访问)
//      //initParameters.put("deny", "192.168.20.38");// IP黑名单 (存在共同时，deny优先于allow)
//      servletRegistrationBean.setInitParameters(initParameters);
//      return servletRegistrationBean;
//    }
//    
//    @Bean
//    public FilterRegistrationBean filterRegistrationBean() {
//      FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
//      filterRegistrationBean.setFilter(new WebStatFilter());
//      filterRegistrationBean.addUrlPatterns("/*");
//      filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
//      return filterRegistrationBean;
//    }
}
